//
//  HLSongListTableController.m
//  HLMusicBox
//
//  Created by hulinhu on 2017/12/28.
//  Copyright © 2017年 hulinhu. All rights reserved.
//

#import "HLSongListTableController.h"
#import <MJRefresh/MJRefresh.h>
#import "HLSongListModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "HLSongListTableCell.h"
#import "HLSongViewModel.h"
#import "HLSongItemModel.h"

@interface HLSongListTableController ()


@property (nonatomic, strong) NSMutableArray *arrayList;

@property (nonatomic, strong) HLSongViewModel *songViewModel;

@end

@implementation HLSongListTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];

}
-(void)setupTableView{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer.hidden = YES;

    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_header beginRefreshing];

}


#pragma mark
#pragma mark - network
-(void)loadData{
    [self requestNetWorkType:1];
}
-(void)loadMoreData {
    [self requestNetWorkType:2];
}

-(void)requestNetWorkType:(NSInteger)refreshType {
    
    NSInteger type = [self getMusicChannelType];
    @weakify(self);
    NSDictionary *parms = @{@"type":@(type), @"offset":@(self.arrayList.count)};
    [[self.songViewModel.songListCommand execute:parms] subscribeNext:^(NSArray *dataArray) {
        NSArray *songArray = [[NSArray yy_modelArrayWithClass:[HLSongListModel class] json:dataArray] mutableCopy];
        @strongify(self);
        if (refreshType == 1) {
            self.arrayList = [songArray mutableCopy];
            [self.tableView.mj_header endRefreshing];
            self.tableView.mj_footer.hidden = NO;
        }else if (refreshType ==2){
            if (self.arrayList.count >= 100) {
                [self.tableView.mj_footer endRefreshing];
                return;
            }
            [self.arrayList addObjectsFromArray:songArray];
            [self.tableView.mj_footer endRefreshing];
        }else {
        }
        [self.tableView reloadData];
        
    }];;
}


#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuseIdentifier = @"HLSongListTableCell";
    HLSongListTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell =  [[[NSBundle mainBundle] loadNibNamed:@"HLSongListTableCell" owner:self options:nil] lastObject] ;
    }
    HLSongListModel *model = self.arrayList[indexPath.row];
    cell.songName.text = model.title;
    cell.singerName.text = model.author;
    [cell.IconImage sd_setImageWithURL:[NSURL URLWithString:model.pic_small] placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([self.channelTitle isEqualToString:@"新歌"]) {
        return self.arrayList.count;
    }else if ([self.channelTitle  isEqual: @"热歌"]) {
        return self.arrayList.count;
    }else if ([self.channelTitle  isEqual: @"经典"]) {
        return self.arrayList.count;
    }else if ([self.channelTitle  isEqual: @"情歌"]) {
        return self.arrayList.count;
    }else if ([self.channelTitle  isEqual: @"网络"]) {
        return self.arrayList.count;
    }else if ([self.channelTitle  isEqual: @"影视"]) {
        return self.arrayList.count;
    }else if ([self.channelTitle  isEqual: @"欧美"]) {
        return self.arrayList.count;
    }else if ([self.channelTitle  isEqual: @"Bill"]) {
        return self.arrayList.count;
    }else if ([self.channelTitle  isEqual: @"摇滚"]) {
        return self.arrayList.count;
    }else if ([self.channelTitle  isEqual: @"爵士"]) {
        return self.arrayList.count;
    }else if ([self.channelTitle  isEqual: @"流行"]) {
        return self.arrayList.count;
    }
    
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 点击歌曲去请求歌曲信息
    HLSongListModel *info = (self.arrayList)[indexPath.row];
    NSLog(@"你选择了《%@》这首歌", info.title);
     HLSongPlayManager *playManager = [HLSongPlayManager shareInstance];
     playManager.songListArray = self.arrayList;
    [playManager requestCurrentPlaySong:info.song_id playSongIndex:indexPath.row];
}


-(NSMutableArray *)arrayList {
    if (!_arrayList) {
        _arrayList = [[NSMutableArray alloc] init];
    }
    return _arrayList;
}
#pragma mark
#pragma mark - 获取音乐类型

-(NSInteger)getMusicChannelType {
    NSInteger type;
    if ([self.channelTitle  isEqual: @"新歌"]) {
        type = NEW_SONG_LIST;
    }else if ([self.channelTitle  isEqual: @"热歌"]) {
        type = HOT_SONG_LIST;
    }else if ([self.channelTitle  isEqual: @"经典"]) {
        type = OLD_SONG_LIST;
    }else if ([self.channelTitle  isEqual: @"情歌"]) {
        type = LOVE_SONG_LIST;
    }else if ([self.channelTitle  isEqual: @"网络"]) {
        type = INTERNET_SONG_LIST;
    }else if ([self.channelTitle  isEqual: @"影视"]) {
        type = MOVIE_SONG_LIST;
    }else if ([self.channelTitle  isEqual: @"欧美"]) {
        type = EUROPE_SONG_LIST;
    }else if ([self.channelTitle  isEqual: @"Bill"]) {
        type = BILLBOARD_MUSIC_LIST;
    }else if ([self.channelTitle  isEqual: @"摇滚"]) {
        type = ROCK_MUSIC_LIST;
    }else if ([self.channelTitle  isEqual: @"爵士"]) {
        type = JAZZ_MUSIC_LIST;
    }else if ([self.channelTitle  isEqual: @"流行"]) {
        type = POP_MUSIC_LIST;
    }else {
        return 0;
    }
    return type;
    
}
-(HLSongViewModel *)songViewModel {
    
    if (!_songViewModel) {
        _songViewModel = [[HLSongViewModel alloc] init];
    }
    return _songViewModel;
    
}
@end
